package com.shangma.utils;

import com.shangma.entity.User;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class RestTemplateTest {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://localhost:8082/common/delete/{0}";
        HttpHeaders headers = new HttpHeaders();
        headers.add("token","HFKJHDJHUI*EHFKLHYFUEFKJ");

        HttpEntity requestEntity = new HttpEntity(headers);
        ResponseEntity<String> exchange = restTemplate.exchange(path, HttpMethod.DELETE, requestEntity, String.class,"1,2");
        System.out.println(exchange);
    }

    private static void restfulMethod() {
        RestTemplate restTemplate = new RestTemplate();
        String phone = "184567435";
//      String path = "http://localhost:8082/common/doLogin/" + phone;
        String path = "http://localhost:8082/common/doLogin/{0}/{1}";
        String forObject = restTemplate.getForObject(path, String.class,phone,"1234");
        System.out.println(forObject);
    }

    private static void getMethod() {
        //        // post和put请求默认发送的是json格式的数据
        RestTemplate restTemplate = new RestTemplate();
        String path = "http://api.tianapi.com/saylove/index?key=7c4e55e6efd90f0baa513c07bcbffbfb";
        // 获取响应全部信息
        ResponseEntity<String> forEntity = restTemplate.getForEntity(path, String.class);
        // 获取响应头
        System.out.println(forEntity.getStatusCode());
        // 获取响应头
        HttpHeaders headers = forEntity.getHeaders();
        System.out.println(headers.get("Connection"));

        // 获取响应体
        String body = forEntity.getBody();
        System.out.println(body);
    }
}
